package com.itheima.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Bean.Orders;
import com.itheima.Service.OrdersService;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import sun.nio.cs.ThreadLocalCoders;

import java.awt.*;
import java.awt.print.Pageable;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController  {

    @Autowired
    private OrdersService ordersService;


    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }


    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                String number,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){

        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getNumber,number);
        lqw.between(beginTime != null && endTime != null,Orders::getOrderTime,beginTime,endTime);
        ordersService.page(ordersPage, lqw);
        return R.success(ordersPage);
    }

    @PutMapping
    public R<String> setStatus(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> luw = new LambdaUpdateWrapper<>();
         luw.eq(orders.getId()!=null,Orders::getId,orders.getId());
        luw.set(Orders::getStatus,orders.getStatus());
        ordersService.update(luw);
        return R.success("更改订单状态成功！！");
    }

    @GetMapping("/userPage")
    public R<Page<Orders>> userpage(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize){
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> lqw =  new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,Orders::getUserId,userId);
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,lqw);
        return R.success(ordersPage);
    }
}
